package com.yangjun.common.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Workbook;

import com.google.common.collect.Lists;
import com.yangjun.entity.report.ys.CreatorGroupWareTaskEntity;

public class ExcelContext {
	private ExcelDefinitionReader definitionReader;
	private ExcelImport excelImport;
	private ExcelExport excelExport;
	
	public ExcelContext(String location){
		definitionReader = new XMLExcelDefinitionReader(location);
		excelImport = new ExcelImport(definitionReader);
		excelExport = new ExcelExport(definitionReader);
	}
	
	public List<String> queryExcelIdList(){
		List<String> excelIdList = Lists.newArrayList();
		Map<String,ExcelDefinition> excelExcelDefinitionMap =  definitionReader.getRegistry();
		for(Map.Entry<String , ExcelDefinition> ExcelDefinitionEntry:excelExcelDefinitionMap.entrySet()){
			excelIdList.add(ExcelDefinitionEntry.getKey());
		}
		return excelIdList;
	}
	/***
	 * 读取Excel信息
	 * @param id 配置ID
	 * @param titleIndex 标题索引,从0开始
	 * @param excelStream Excel文件流
	 * @return ExcelImportResult
	 * @throws Exception 
	 */
	public ExcelImportResult readExcel(String id,int titleIndex, InputStream excelStream) throws Exception {
		return excelImport.readExcel(id,titleIndex, excelStream,null);
	}
	/**
	 * 
	 * @Description 创建Excel
	 * @param id 配置的ID
	 * @param beans 对应配置CLASS的List
	 * @return
	 * @author yangjun
	 * @Date 2017年6月23日 上午9:35:05
	 */
	public Workbook createExcel(String id,List<?> beans){
		return createExcel(id,beans,null);
	}
	/**
	 * @Description (TODO这里用一句话描述这个方法的作用)
	 * @param id 配置的ID
	 * @param beans 对应配置CLASS的List
	 * @param fields 指定导出的字段
	 * @return
	 * @author yangjun
	 * @Date 2017年6月23日 上午9:36:27
	 */
	public Workbook createExcel(String id,List<?> beans,List<String> fields){
		return excelExport.createExcel(id, beans,fields).build();
	}
	
	
	public ExcelDefinitionReader getDefinitionReader() {
		return definitionReader;
	}

	public void setDefinitionReader(ExcelDefinitionReader definitionReader) {
		this.definitionReader = definitionReader;
	}

	public ExcelImport getExcelImport() {
		return excelImport;
	}

	public void setExcelImport(ExcelImport excelImport) {
		this.excelImport = excelImport;
	}

	public ExcelExport getExcelExport() {
		return excelExport;
	}

	public void setExcelExport(ExcelExport excelExport) {
		this.excelExport = excelExport;
	}

//	public static void main(String[] args) {
////		ClassPathResource resource = new ClassPathResource("");
//		InputStream inputStream = null;
//		try {
//			inputStream = new FileInputStream(new File("D:/eclipse2016/upload/0b510f8c-0445-4a2d-99d5-1897cd4b5216YS-药师中心-7月份单品任务分解表-王国丽.xlsx"));
//			ExcelContext context = new ExcelContext("excel-config.xml");
//			ExcelImportResult result= context.readExcel("ysr1", 0, inputStream);
//			List<CreatorGroupWareTaskEntity> list = result.getListBean();
//			for(CreatorGroupWareTaskEntity entity : list){
//				System.out.println(entity.getCreatorGroup()+" "+entity.getGoodid()+" "+entity.getGoodsname()+" "+entity.getTask()+" "+entity.getTaskDate());
//			}
//			System.out.println(list.size());
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		}catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
}
